jQuery["post"] = function (url, data, callback, type) {
    type = type || "json";
    if (jQuery.isFunction(data)) {
        type = type || callback;
        callback = data;
        data = undefined;
    }
    return jQuery.ajax({
        type: "post",
        url: url,
        data: data,
        success: callback,
        dataType: type
    });
};

/***错误处理 ***/
$.ajaxSetup({cache: false,
    error: function (XMLHttpRequest, textStatus, errorThrown) {
        // 登入超时
        if (XMLHttpRequest.status == "401") {
            window.top.location.href = ctx + "/index.jsp";
        } else {
            if(XMLHttpRequest.responseText){
                window.top.$.pnotify({
                    title: '服务器错误',
                    text: XMLHttpRequest.responseText,
                    type: 'error',
                    animation: 'fade',
                    hide: false,
                    shadow: false
                });
            }
        }
    }
});

String.prototype.replaceAll = function (s1, s2) {
    return this.replace(new RegExp(s1, "gm"), s2);
}

function fomatDate(date) {
    if (!date) {
        return "";
    }
    return date.replace(/\s*\d{2}:.*/, "");
}

/*** 应用工具类 ****/
window.appUtil = {
    createEditor:function(target,optionset){ // 创建内容编辑器
        KindEditor.ready(function(K) {
            window.editor = K.create('textarea[name="'+target+'"]', {
                uploadJson : ctx+"/js/kindeditor/jsp/upload_json.jsp",
                fileManagerJson : ctx+"/js/kindeditor/jsp/file_manager_json.jsp",
                allowFileManager : false,
                resizeType : 0,
                items : [
                    'source', 'fontsize', 'forecolor', 'bold', '|', 'justifyleft', 'justifycenter', 'justifyright',  '|','link', 'unlink', 'image','baidumap']
            });
        });
    }
};

/*** 图片预览 ***/
function previewImage(file, target, w, h, fn) {
    if (fn && typeof(fn == "function")) {
        if (!fn.apply()) {
            return;
        }
    }
    var div = $("#" + target);
    div.show();
    var MAXWIDTH = w || div.width() || 100;
    var MAXHEIGHT = h || div.height() || 100;
    div.html('<img style="vertical-align: top;width:' + MAXWIDTH + 'px;height:' + MAXHEIGHT + 'px;filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=image);">');
    var img = div.find("img")[0];
    if (file.files && file.files[0]) {
        img.onload = function () {
            var rect = clacImgZoomParam(MAXWIDTH, MAXHEIGHT, img.offsetWidth, img.offsetHeight);
            img.width = rect.width;
            img.height = rect.height;
        };
        var reader = new FileReader();
        reader.onload = function (evt) {
            img.src = evt.target.result;
        };
        reader.readAsDataURL(file.files[0]);
    } else {
        var sFilter = 'filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale,src="';
        file.select();
        var src = document.selection.createRange().text;
        img.filters.item('DXImageTransform.Microsoft.AlphaImageLoader').src = src;
        var rect = clacImgZoomParam(MAXWIDTH, MAXHEIGHT, img.offsetWidth, img.offsetHeight);
        status = ('rect:' + rect.top + ',' + rect.left + ',' + rect.width + ',' + rect.height);
        div.html("<div style='width:" + rect.width + "px;height:" + rect.height + "px;" + sFilter + src + "\"'></div>");
    }
    var clacImgZoomParam = function (maxWidth, maxHeight, width, height) {
        var param = {top: 0, left: 0, width: width, height: height};
        if (width > maxWidth || height > maxHeight) {
            rateWidth = width / maxWidth;
            rateHeight = height / maxHeight;
            if (rateWidth > rateHeight) {
                param.width = maxWidth;
                param.height = Math.round(height / rateWidth);
            } else {
                param.width = Math.round(width / rateHeight);
                param.height = maxHeight;
            }
        }
        param.left = Math.round((maxWidth - param.width) / 2);
        param.top = Math.round((maxHeight - param.height) / 2);
        return param;
    };
}

$(document).ready(function () {
    $("input[type=text]", $("form")).eq(0).focus();
});


